package net
{
	import flash3.net.ByteArrayPlus;
	import flash3.net.MsgPacket;
	import flash3.net.NetFacade;
	
	import model.MeInfoModel;
	
	import net.MessageDef;
	
	import util.MD5;
	
	import vo.*;
	
	public class LobbyLoginGroup extends MessageGroup
	{
		public static const MDM_GP_LOGON:uint					= 	100								//广场登录
		
		public static const SUB_GP_LOGON_ACCOUNTS:uint		=	1								//帐号登录
		public static const SUB_GP_LOGON_USERID:uint			=	2								//I D 登录
		public static const SUB_GP_REGISTER_ACCOUNTS:uint		=	3								//注册帐号
		
		public static const SUB_GP_LOGON_SUCCESS:uint			=	100								//登陆成功
		public static const SUB_GP_LOGON_ERROR:uint			=	101								//登陆失败
		public static const SUB_GP_LOGON_FINISH:uint			=	102								//登陆完成
			
		//基础信息
		public static const  DTP_USER_ID:uint					=	1								//用户 I D
		public static const  DTP_GAME_ID:uint					=	2								//用户 I D
		public static const 	DTP_USER_ACCOUNTS:uint			=	3								//用户帐号
		public static const 	DTP_USER_PASS:uint				=	4								//用户密码
		public static const  DTP_USER_GENDER:uint				=	5								//用户性别
		public static const  DTP_USER_FACE:uint				=	6								//用户头像
		public static const  DTP_USER_RIGHT:uint				=	7								//用户权限
		public static const  DTP_MASTER_RIGHT:uint			=	8								//管理权限
		public static const 	DTP_UNDER_WRITE:uint			=	9								//个性签名
		public static const 	DTP_FACE_URL:uint				=	10								//头像连接
			
		//状态信息
		public static const DTP_USER_STATUS:uint				=	100								//用户状态
		public static const DTP_USER_TABLE:uint				=	101								//游戏桌号
		public static const DTP_USER_CHAIR:uint				=	102								//椅子号码
		public static const DTP_USER_PING:uint				=	103								//网络延时
		public static const DTP_USER_INDEX:uint				=	104								//用户索引
		public static const DTP_USER_ROUND:uint				=	105								//循环计数
		
		//积分信息
		public static const DTP_USER_WIN:uint					=200								//胜局盘数
		public static const DTP_USER_LOST:uint				=	201								//输局盘数
		public static const DTP_USER_DRAW	:uint				=202								//和局盘数
		public static const DTP_USER_FLEE:uint				=	203								//逃局盘数
		public static const DTP_WIN_RATE:uint					=204								//用户胜率
		public static const DTP_LOST_RATE:uint				=205								//用户输率
		public static const DTP_DRAW_RATE	:uint				=206								//用户和率
		public static const DTP_FLEE_RATE	:uint				=207								//用户逃率
		public static const DTP_USER_TAX:uint					=208								//用户税收
		public static const DTP_USER_GOLD:uint				=	209								//用户金币
		public static const DTP_USER_SCORE:uint				=	210								//用户分数
		public static const DTP_USER_PLAY_COUNT:uint			=	211								//总局盘数
		public static const DTP_USER_EXPERIENCE:uint			=	212								//用户经验
		public static const DTP_GAME_LEVEL:uint				=	213								//游戏等级
		public static const DTP_COMMUNITY_LEVEL:uint			=	214								//社区等级
		
		//扩展信息
		public static const DTP_USER_GROUP_ID:uint			=	300								//社团 I D
		public static const DTP_USER_GROUP_NAME:uint			=	301								//社团名字
		public static const DTP_USER_NOTE	:uint				=302								//用户备注
		public static const DTP_USER_DESCRIBE:uint			=	303								//用户描述
		public static const DTP_USER_COMPANION:uint			=	304								//用户关系
		
		//系统信息
		public static const DTP_COMPUTER_ID:uint				=	1000							//机器序列
		public static const DTP_STATION_PAGE:uint				=1001							//站点页面
		public static const DTP_OPTION_BUFFER	:uint			=1002							//配置信息
		
		public function LobbyLoginGroup()
		{
			super();
		}
		
		override public function defMessage():void
		{
			addMessage(new MessageDef(MDM_GP_LOGON, SUB_GP_LOGON_ACCOUNTS, "账户登录", onLoginAccounts));
			addMessage(new MessageDef(MDM_GP_LOGON, SUB_GP_LOGON_USERID, "I D 登录", onLoginUserID));
			addMessage(new MessageDef(MDM_GP_LOGON, SUB_GP_REGISTER_ACCOUNTS, "注册帐号", onRegisterAccounts));
			addMessage(new MessageDef(MDM_GP_LOGON, SUB_GP_LOGON_SUCCESS, "登陆成功", onLoginSuccess));
			addMessage(new MessageDef(MDM_GP_LOGON, SUB_GP_LOGON_ERROR, "登陆失败", onLoginError));
			addMessage(new MessageDef(MDM_GP_LOGON, SUB_GP_LOGON_FINISH, "登陆完成", onLoginFinish));
		}
		
		public function onLoginAccounts(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			var loginInfo:LoginInfo = packet._parm as LoginInfo;
			bap.writeUnsignedInt(ExtrInfo.dwPlazaVersion);
			bap.writeString({value:loginInfo.szAccounts,length:32});
			bap.writeString({value:loginInfo.szCoprName,length:32});
			bap.writeString({value:loginInfo.szPassWord,length:33});
			bap.writeShort(ClientSerial.STRUCT_LEN);
			bap.writeShort(ExtrInfo.computerId);
			bap.writeUnsignedInt(ClientSerial.dwSystemVer);
			bap.writeUnsignedInt(ClientSerial.dwComputerID[0]);
			bap.writeUnsignedInt(ClientSerial.dwComputerID[1]);
			bap.writeUnsignedInt(ClientSerial.dwComputerID[2]);
		}
		public function onLoginUserID(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			var loginInfo:LoginInfo = packet._parm as LoginInfo;
			bap.writeUnsignedInt(ExtrInfo.dwPlazaVersion);
			bap.writeUnsignedInt(loginInfo.dwUserID);
			bap.writeString({value:loginInfo.szPassWord,length:33});
			bap.writeShort(ClientSerial.STRUCT_LEN);
			bap.writeShort(ExtrInfo.computerId);
			bap.writeUnsignedInt(ClientSerial.dwSystemVer);
			bap.writeUnsignedInt(ClientSerial.dwComputerID[0]);
			bap.writeUnsignedInt(ClientSerial.dwComputerID[1]);
			bap.writeUnsignedInt(ClientSerial.dwComputerID[2]);
		}
		public function onRegisterAccounts(packet:MsgPacket, data:* = null):void
		{
			
		}
		public function onLoginSuccess(packet:MsgPacket):void
		{
			var bap:ByteArrayPlus = packet._data;
			
			var userInfo:UserInfo = MeInfoModel.i.userInfo;
			var userScore:UserScore = MeInfoModel.i.userScore;
			userInfo.wFaceID = bap.readShort();
			userInfo.cbGender = bap.readByte();
			userInfo.cbMemberOrder = bap.readByte();
			userInfo.dwUserID = bap.readUnsignedInt();
			userInfo.dwGameID = bap.readUnsignedInt();
			userScore.lExperience = bap.readUnsignedInt();
			userScore.lGoldScore = bap.readUnsignedInt();
			
			while(bap.bytesAvailable > 2)
			{
				var wDataSize:uint = bap.readShort();
				var wDataType:uint = bap.readShort();
				switch(wDataType)
				{
					case DTP_USER_ACCOUNTS:		//用户帐户
					{
						MeInfoModel.i.szAccounts = bap.readUTFBytes(wDataSize);
						userInfo.szAccounts = MeInfoModel.i.szAccounts;
					}
						break;
					case DTP_USER_PASS:			//用户密码
					{
						var md:String = MeInfoModel.i.mdPassWord = bap.readUTFBytes(wDataSize);
					}
						break;
					case DTP_UNDER_WRITE:		//个性签名
					{
						userInfo.szUnderWrite = bap.readUTFBytes(wDataSize);
					}
						break;
					case DTP_USER_GROUP_NAME:	//社团名字
					{
						userInfo.szGroupName = bap.readUTFBytes(wDataSize);
					}
						break;
					case DTP_STATION_PAGE:		//游戏主站
					{
						ExtrInfo.szGameMainIP = bap.readUTFBytes(wDataSize);
					}
						break;
					case DTP_FACE_URL:			//头像连接
					{
						userInfo.szImageUrl = bap.readUTFBytes(wDataSize);
					}
						break;
					default:
						bap.position += wDataSize;
						break;
				}
			}
		}
		public function onLoginError(packet:MsgPacket):void
		{
			
		}
		public function onLoginFinish(packet:MsgPacket):void
		{
		}
	}
}